import Vue from "vue";
import VueRouter from "vue-router";
import updateInfo from "@/components/updateInfo";

Vue.use(VueRouter);

const routes = [
  {
    // 随便定义的首页
    path: "/",
    name: "index",
    component: () => import("@/views/login/index"),
    redirect: "/login",
  },
  {
    // 登陆页
    path: "/login",
    name: "login",
    component: () => import("@/views/login/index"),
    meta: { title: "登录" },
  },
  {
    // admin 的路由
    path: "/admin",
    name: "admin",
    redirect: "/adminHome",
    component: () => import("@/views/Admin/index"),
    meta: { title: "admin 主页", requireAuth: true },
    children: [
      {
        path: "/adminHome",
        name: "Hi! admin",
        component: () => import("@/views/Admin/home"),
        meta: { title: "admin 主页", requireAuth: true },
        children: [
          {
            path: "/adminHome",
            name: "admin 主页",
            component: () => import("@/views/Admin/home"),
            meta: { title: "admin 主页", requireAuth: true },
          },
        ],
      },
      {
        path: "/userData",
        name: "用户信息",
        component: () => import("@/views/Admin/userDataManage/index.vue"),
        meta: { title: "用户信息", requireAuth: true },
        children: [
          {
            path: "/userData",
            name: "用户信息",
            component: () => import("@/views/Admin/userDataManage/index.vue"),
            meta: { title: "用户信息", requireAuth: true },
          },
        ],
      },
      {
        path: "/checkRecord",
        name: "检测记录",
        component: () => import("@/views/Admin/checkRecordManage/index.vue"),
        meta: { title: "检测记录", requireAuth: true },
        children: [
          {
            path: "/checkRecord",
            name: "检测记录",
            component: () =>
              import("@/views/Admin/checkRecordManage/index.vue"),
            meta: { title: "检测记录", requireAuth: true },
          },
        ],
      },
    ],
  },
  {
    path: "/user",
    name: "user",
    component: () => import("@/views/User/index.vue"),
    redirect: "/userHome",
    meta: { title: "用户主页", requireAuth: true },
    children: [
      {
        path: "/userHome",
        name: "Hi! user",
        meta: { title: "用户主页", requireAuth: true },
        component: () => import("@/views/User/home.vue"),
        children: [
          {
            path: "/userHome",
            name: "用户主页",
            meta: { title: "用户主页", requireAuth: true },
            component: () => import("@/views/User/home.vue"),
          },
        ],
      },
      {
        path: "/userDetect",
        name: "检测",
        meta: { title: "检测", requireAuth: true },
        component: () => import("@/views/User/detectionPage/index.vue"),
        children: [
          {
            path: "/userDetect",
            name: "检测",
            meta: { title: "检测", requireAuth: true },
            component: () => import("@/views/User/detectionPage/index.vue"),
          },
        ],
      },
    ],
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

export default router;

/*
  session 设置：
    1. token
    2. name
    3. type
    4. tid
    5. sid
    5. 系统信息 info
 */
router.beforeEach((to, from, next) => {
  console.log(from.path + " ====> " + to.path);
  // if (to.meta.requireAuth) {
  //   // 判断该路由是否需要登录权限
  //   if (sessionStorage.getItem("token") === "true") {
  //     // 判断本地是否存在token
  //     next();
  //   } else {
  //     // 未登录,跳转到登陆页面
  //     next({
  //       path: "/login",
  //       query: { redirect: to.fullPath },
  //     });
  //   }
  // } else {
  //   // 不需要登陆权限的页面，如果已经登陆，则跳转主页面
  //   if (sessionStorage.getItem("token") === "true") {
  //     console.log("检查拦截器配置，大概率出现漏网之鱼");
  //     const t = sessionStorage.getItem("type");
  //     next("/" + t);
  //   } else {
  //     next();
  //   }
  // }
  if (to.meta.title) {
    document.title = to.meta.title;
  } else {
    // document.title = APP.title;
  }
  next();
});
